/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.zwu.t2;

import java.math.BigDecimal;
import javax.jcr.*;
import javax.jcr.nodetype.*;
import javax.jcr.query.*;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.jackrabbit.core.TransientRepository;

/**
 *
 * @author Administrator
 */
public class ILookImportFileSQL {

    public static void main(String[] args) throws Exception {
        Repository repository = new TransientRepository();
        Session session = repository.login(
                new SimpleCredentials("username", "password".toCharArray()));
        try {
            Node root = session.getRootNode();

            // Store content
            Node hello = root.addNode("hello");
            Node world = hello.addNode("world");
            world.setProperty("message", "Hello, World!");
            session.save();

            // Retrieve content
            Node node = root.getNode("hello/world");
            System.out.println(node.getPath());
            System.out.println(node.getProperty("message").getString());

            mynode(session.getWorkspace());
            // Remove content

            root.getNode("hello").remove();
            session.save();
        } finally {
            session.logout();
        }
    }

    public static void mynode(Workspace wk) throws Exception {
        NodeTypeManager ntm = wk.getNodeTypeManager();
        NodeTypeTemplate ntt = ntm.createNodeTypeTemplate();

        String tagName = "mytag";

        ntt.setName(tagName);
        ntt.setDeclaredSuperTypeNames(new String[]{"nt:unstructured"});
        //ntt.setPrimaryItemName("nt:unstructured");
        ntm.registerNodeType(ntt, true);
        System.out.println(ntm.hasNodeType(tagName));
        //Node node = wk.getNamespaceRegistry().registerNamespace(, null)


        Node n = wk.getSession().getRootNode();
        //n = n.addNode("myfolder1","nt:folder" );
        //n.setProperty("any", BigDecimal.ONE);
        //n = n.addNode("nothing", "nt:file");
        //n.addMixin("ilooktag2");
        //n = n.addNode("testing", tagName);
        //n.setProperty("first", "zhou wu");
        //n.setProperty("nt:file", "zhou");

        QueryManager qm = wk.getQueryManager();

        //Query q = qm.createQuery("select first from  mytag where first = 'zhou'", Query.SQL);
        Query q = qm.createQuery("select * from  nt:resource where contains(., 'zhou')", Query.SQL);
        QueryResult result = q.execute();
        for (RowIterator it = result.getRows(); it.hasNext();) {
            Row r = it.nextRow();
            PropertyIterator pi = r.getNode().getProperties();
            while (pi.hasNext()) {
            System.out.println(pi.nextProperty().getName());
            }
            Value excerpt = r.getValue("rep:excerpt(.)");
            System.out.println(excerpt.getString());
            excerpt = r.getValue("rep:excerpt(jcr:data)");
            System.out.println(excerpt.getString());
        }
    }
}
